"""
创建一个progressBar class
循环增加长度 并调用绘画函数显示百分比
"""
import sys
import time


class ProgressBar:
    def __init__(self, total_len, bar_char='◼', empty_char=' '):
        self.current_len = 0
        self.total_len = total_len
        self.bar_char = bar_char
        self.empty_char = empty_char
        self.write_progress = sys.stdout
        vstr = '-' * 45
        self.write_progress.write(vstr + '显示进度条' + vstr)
        if not total_len:
            return

    def draw(self):
        if self.current_len > self.total_len:
            percent_int = 100
        percent_int = self.current_len * 100 // self.total_len
        bar_str = self.bar_char * percent_int
        space_str = self.empty_char * (100 - percent_int)
        self.write_progress.write('\r' + bar_str + space_str + '%' + str(percent_int))
        self.write_progress.flush()

    def add_len(self, step_len):
        self.current_len += step_len
        self.draw()


if __name__ == '__main__':
    progressBar = ProgressBar(total_len=100)
    for i in range(100):
        time.sleep(.05)
        progressBar.add_len(1)
